package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.TimeCount;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.BankSelectorWindow;
import com.lanlian.cayman.view.CitySelectorWindow;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.CityInfo;
import com.lanlian.commonlib.bean.ProvinceInfo;
import com.lanlian.commonlib.bean.ReqSmsCodeForBank;
import com.lanlian.commonlib.bean.ReqUpdateBank;
import com.lanlian.commonlib.bean.RespSmsCode;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.utillib.WorksSizeCheckUtil;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/23 10:28
 *     desc   : 修改银行卡
 *     version: 1.0
 * </pre>
 */
public class UpdateBankActivity extends BaseActivity implements View.OnClickListener {

    private EditText mEtBankCard;
    private TextView mTvBankCity;
    private TextView mTvBankType;
    private EditText mEtBankBranch;
    private TextView mTvMobile;
    private TextView mTvCode;
    private EditText mEtCode;
    private TextView mTvSubmit;

    private CitySelectorWindow mCitySelectorWindow;
    private BankSelectorWindow mBankSelectorWindow;

    private ReqUpdateBank mReqUpdateBank;

    private TimeCount mTimeCount;
    private String mValKey;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_bank, true);
        initView();
        setListeners();
        mReqUpdateBank = new ReqUpdateBank();
        if (mDBManager.getUser() != null) {
            mTvMobile.setText(mDBManager.getUser().getMobile());
        }
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("修改银行卡");
        mEtBankCard = findViewById(R.id.et_bank_card);
        mTvBankCity = findViewById(R.id.tv_bank_city);
        mTvBankType = findViewById(R.id.tv_bank_type);
        mEtBankBranch = findViewById(R.id.et_bank_branch);
        mTvMobile = findViewById(R.id.tv_mobile);
        mTvCode = findViewById(R.id.tv_code);
        mEtCode = findViewById(R.id.et_code);
        mTvSubmit = findViewById(R.id.tv_submit);
        mTimeCount = new TimeCount(this, 60000, 1000, mTvCode);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvSubmit.setOnClickListener(this);
        mTvBankCity.setOnClickListener(this);
        mTvBankType.setOnClickListener(this);
        mTvCode.setOnClickListener(this);
        WorksSizeCheckUtil worksSizeCheckUtil = new WorksSizeCheckUtil(mTvSubmit);
        worksSizeCheckUtil.addAllEditText(mEtBankBranch, mEtCode, mEtBankCard, mTvBankCity, mTvBankType);
        worksSizeCheckUtil.setChangeListener(new WorksSizeCheckUtil.IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if (isHasContent) {
                    mTvSubmit.setBackgroundResource(R.mipmap.login_btn);
                } else {
                    mTvSubmit.setBackgroundResource(R.drawable.bg_click_false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_bank_city:
                if (mCitySelectorWindow == null) {
                    mCitySelectorWindow = new CitySelectorWindow(this);
                    mCitySelectorWindow.setOnCityChooseListener(new CitySelectorWindow.OnCityChooseListener() {
                        @Override
                        public void onCityChoose(final ProvinceInfo provinceInfo, final CityInfo cityInfo) {
                            mTvBankCity.setText(String.format("%s %s", provinceInfo.getName(), cityInfo.getName()));
                            mReqUpdateBank.setBankCity(cityInfo.getId());
                            mReqUpdateBank.setBankProvince(provinceInfo.getId());
                        }
                    });
                }
                mCitySelectorWindow.showDialog();
                break;
            case R.id.tv_bank_type:
                if (mBankSelectorWindow == null) {
                    mBankSelectorWindow = new BankSelectorWindow(this);
                    mBankSelectorWindow.setOnBankChooseListener(new BankSelectorWindow.OnBankChooseListener() {
                        @Override
                        public void onBankChoose(UtilEnumInfo bank) {
                            mTvBankType.setText(bank.getName());
                            mReqUpdateBank.setBankType(bank.getId());
                        }
                    });
                }
                mBankSelectorWindow.showDialog();
                break;
            case R.id.tv_submit:
                updateBank();
                break;
            case R.id.tv_code:
                sendSmsCode();
                break;
            default:
                break;
        }
    }

    /**
     * 申请修改银行卡
     */
    private void updateBank() {
        mReqUpdateBank.setId(mDBManager.getUser().getAccountNo());
        mReqUpdateBank.setBankCard(mEtBankCard.getText().toString());
        mReqUpdateBank.setBankBranch(mEtBankBranch.getText().toString());
        mReqUpdateBank.setCode(mEtCode.getText().toString());
        mReqUpdateBank.setValKey(mValKey);
        showLoadingDialog("申请中");
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), mReqUpdateBank, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                showToast("申请成功");
                finish();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                dismissLoadingDialog();
                showToast(baseResponse.getMsg());
            }
        });
    }

    /**
     * 发送手机验证码
     */
    private void sendSmsCode() {
        mTvCode.setClickable(false);
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), new ReqSmsCodeForBank(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                RespSmsCode respSmsCode = (RespSmsCode) response;
                mValKey = respSmsCode.getValKey();
                mTimeCount.startTime();
                showToast("验证码已发送到你的手机");
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mTvCode.setClickable(true);
                showToast(baseResponse.getMsg());
            }
        });
    }

}
